#include "stdafx.h"

CFen::CFen()
{
}


CFen::~CFen()
{
}

uint CFen::Set( CBoard * board, char * fen )
{
    board->Rst(); 
    char position[256];
    char color[4];
    char castlings[8];
    char enpassant[4];
    char halfmoves[8];
    sscanf_s(fen, "%s %s %s %s %s", position, 256, color, 4, castlings, 8, enpassant, 4, halfmoves, 8 );
    Position( board, position );
    SetCastles( board, castlings );
    SetEnpassant( board, enpassant );
    if ( color[0] == 'b' ) SetColor( board, BLACK );
    else SetColor( board, WHITE );
    return 0;
};

uint CFen::AddPiece( CBoard * board, uint lin, uint col, uint pie )
{
    board->Add(MAILBOX64[COORD2SQUARE(lin,col)], pie );
    return 0;
}

uint CFen::Position( CBoard * board, char * fen )
{    
    uint sq     = 0;
    uint col    = 0;
    uint ln     = 0;
    //cout << "start";
    while( END_OF_STRING(fen[0]) ){
        switch( fen[0] )
        {
            case '/': ln++; col = 0; break;
            case '1': col += 1; break;
            case '2': col += 2; break;
            case '3': col += 3; break;
            case '4': col += 4; break;
            case '5': col += 5; break;
            case '6': col += 6; break;
            case '7': col += 7; break;
            case '8': col += 8; break;
            case 'P': AddPiece( board, ln, col, WP ); col++; break;
            case 'N': AddPiece( board, ln, col, WN ); col++; break;
            case 'B': AddPiece( board, ln, col, WB ); col++; break;
            case 'R': AddPiece( board, ln, col, WR ); col++; break;
            case 'Q': AddPiece( board, ln, col, WQ ); col++; break;
            case 'K': AddPiece( board, ln, col, WK ); col++; break;
            case 'p': AddPiece( board, ln, col, BP ); col++; break;
            case 'n': AddPiece( board, ln, col, BN ); col++; break;
            case 'b': AddPiece( board, ln, col, BB ); col++; break;
            case 'r': AddPiece( board, ln, col, BR ); col++; break;
            case 'q': AddPiece( board, ln, col, BQ ); col++; break;
            case 'k': AddPiece( board, ln, col, BK ); col++; break;
            default : cout << "CFen::Position:skipped"; 
        }
        fen++;
    }
    return 0;
};

uint CFen::SetCastles( CBoard * board, char * c )
{    
    uint castles = 0;
    //cout << "CFen::SetCastles "<<c<<endl;
    while( END_OF_STRING(c[0]) ){
        switch( c[0] )
        {
            case 'K': castles |= CASTLE_WK; break;
            case 'Q': castles |= CASTLE_WQ; break;
            case 'k': castles |= CASTLE_BK; break;
            case 'q': castles |= CASTLE_BQ; break;
            // default : cout << "CFen::SetCastles:skipped"; 
        }
        c++;
    }
    board->SetFlags( castles );
    return 0;
};

uint CFen::SetEnpassant( CBoard * board, char * ep )
{
    for( uint sq = 0; sq < 64; sq++  )
        {
            if ( (strstr( SQ_NAME64[sq], ep )) ) {
                board->SetEnpassant( MAILBOX64[sq] );
                return 0;
            }
        }
    // no enpassant
    board->SetEnpassant( 0 );
    return 0;
};

uint CFen::SetColor( CBoard * board,  uint color )
{
    board->SetColor( color );
    return 0;
};